// ##############################
// ####  note:  bokeweilai lcd main fun
// ####  create:  TANWENHUI (twh:谭文辉)
// ####  date:   2024/6/18
// ##############################

#include "bkwl_common.h"
#include "bkwl_log.h"
#include "bkwl_event.h"
#include "driver/lcd_types.h"
#include <driver/media_types.h>
#include "bkwl_lcd.h"

#include <common/bk_include.h>
#include <os/mem.h>

#include <stdlib.h>
// X:\twh\work\bk7258\bk_avdk\bk_idk\components\tfm\tfm\platform\ext\target\beken\armino\include\driver\hal\hal_uart_types.h

#define pdata_addr (uint32_t *)(0x60000000)
bk_err_t bkwl_lcd_init(char *lcd_type_name, media_ppi_t ppi)
{
	bk_err_t ret = BK_OK;
	tprintf("bkwl_lcd_init entry\n");
	lcd_open_t lcd_open;
	lcd_open.device_ppi = ppi;			  // PPI_800X480;
	lcd_open.device_name = lcd_type_name; //"h050iwv";
	ret = media_app_lcd_open(&lcd_open);
	tprintf("bkwl_lcd_init 1\n");
	if (BK_OK != ret)
	{
		os_printf("%s, not found device\n", __func__);
		ret = BK_FAIL;
	}

	tprintf("bkwl_lcd_init exit\n");
	return ret;
}
bk_err_t bkwl_lcd_fill(uint32_t *addr, uint8_t *data, uint16_t width, uint16_t height)
{
	bk_err_t ret = BK_OK;
	memcpy(addr, data, width * height * 3);
	return ret;
}

bk_err_t bkwl_lcd_display(uint32_t *addr, char *lcd_type_name, media_ppi_t ppi, uint8_t lcd_type, uint16_t width, uint16_t height)
{
	bk_err_t ret = BK_OK;

	tprintf("bkwl_lcd_display entry\n");
	// lcd_open_t lcd_open;
	lcd_display_t lcd_display = {0};
	// lcd_open.device_ppi = ppi;			  // PPI_800X480;
	// lcd_open.device_name = lcd_type_name; //"h050iwv";
	lcd_display.display_type = lcd_type; // LCD_TYPE_MCU8080;
	lcd_display.image_addr = (unsigned int)addr;
	lcd_display.x_start = 0;
	lcd_display.x_end = width - 1;
	lcd_display.y_start = 0;
	lcd_display.y_end = height - 1;

	ret = media_app_lcd_display(&lcd_display);
	if (BK_OK != ret)
	{
		os_printf("%s, lcd display fail\n", __func__);
	}

	tprintf("bkwl_lcd_display exit\n");
	return ret;
}

void bkwl_lcd_get_rand_color(uint32_t *color)
{
	uint32_t color_rand = 0;
	uint32_t color_rand_tmp = 0;

	color_rand = (uint32_t)rand();
	// color_rand = 0x00ff00;
	//  color_rand = 0xff0000;
	// color_rand = 0xffff0000;
	color_rand_tmp = (color_rand & 0xffff0000) >> 16;
	*color = (color_rand & 0xffff0000) | color_rand_tmp;
}
#define RAM_START_ADDRESS 0x400000

#define DATA_START_ADDRESS &_data_ram_begin
#define DATA_END_ADDRESS &_data_ram_end
#define BSS_START_ADDRESS &_bss_start
#define BSS_END_ADDRESS &_bss_end
extern unsigned char _bss_start, _bss_end, _data_ram_begin, _data_ram_end;
unsigned char _empty_ram;
#define HEAP_START_ADDRESS (void *)&_empty_ram
#define HEAP_END_ADDRESS (void *)(0x00400000 + 256 * 1024)

void pvShowMemoryConfigInfo1()
{
	tprintf("\n");
	tprintf("%-8s %-8s %-8s %-8s\n", "mem_type", "start", "end", "size");
	tprintf("%-8s %-8s %-8s %-8s\n", "--------", "--------", "--------", "--------");
	tprintf("%-8s 0x%-6x 0x%-6x %-8d\r\n", "ram", RAM_START_ADDRESS, HEAP_END_ADDRESS, (HEAP_END_ADDRESS - RAM_START_ADDRESS));
	tprintf("%-8s 0x%-6x 0x%-6x %-8d\r\n", "non_heap", RAM_START_ADDRESS, HEAP_START_ADDRESS, (HEAP_START_ADDRESS - RAM_START_ADDRESS));
	tprintf("%-8s 0x%-6x 0x%-6x %-8d\r\n", "data", DATA_START_ADDRESS, DATA_END_ADDRESS, (DATA_END_ADDRESS - DATA_START_ADDRESS));
	tprintf("%-8s 0x%-6x 0x%-6x %-8d\r\n", "bss", BSS_START_ADDRESS, BSS_END_ADDRESS, (BSS_END_ADDRESS - BSS_START_ADDRESS));
	tprintf("%-8s 0x%-6x 0x%-6x %-8d\r\n", "heap", HEAP_START_ADDRESS, HEAP_END_ADDRESS, (HEAP_END_ADDRESS - HEAP_START_ADDRESS));
}
bk_err_t bkwl_lcd_display_red()
{
	bk_err_t ret = BK_OK;

	tprintf("bkwl_lcd_display_red entry\n");
	// uint16_t i = 0;
	uint32_t *addr = pdata_addr;
	char *lcd_type_name = "h050iwv";
	media_ppi_t ppi = PPI_800X480;
	uint8_t lcd_type = LCD_TYPE_MCU8080;
	uint16_t width = 800;
	uint16_t height = 480;
	tprintf("bkwl_lcd_display_red 1\n");
	// uint8_t *show_data = (uint8_t *)os_malloc(width * height * 1 + 1);
	// tprintf("bkwl_lcd_display_red 2 show_data=%p, %x\n", show_data, show_data);
	// uint16_t num = 0;
	// for (i = 0; i < width * height; i++)
	// {
	// 	uint32_t color = 0xff0000;
	// 	color = (uint32_t)rand();
	// 	tprintf("bkwl_lcd_display_red 3 1 i=%d\n", i);
	// 	uint8_t value = (uint8_t)((color >> 16) & 0xff);
	// 	tprintf("bkwl_lcd_display_red 3 1 2\n");
	// 	num = (i * 3 + 0);
	// 	tprintf("bkwl_lcd_display_red 3 1 3 a show_data=%p,num=%d\n", show_data, num);
	// 	show_data = show_data + num;
	// 	tprintf("bkwl_lcd_display_red 3 1 3 b show_data=%p,num=%d\n", show_data, num);
	// 	*(show_data) = value;
	// 	tprintf("bkwl_lcd_display_red 3 1 4\n");
	// 	value = (uint8_t)((color >> 8) & 0xff);
	// 	tprintf("bkwl_lcd_display_red 3 1 5\n");
	// 	num = (i * 3 + 1);
	// 	tprintf("bkwl_lcd_display_red 3 1 6 show_data=%p,num=%d\n", show_data, num);
	// 	show_data = show_data + num;
	// 	*(show_data) = value;
	// 	tprintf("bkwl_lcd_display_red 3 1 7\n");
	// 	value = (uint8_t)((color) & 0xff);
	// 	tprintf("bkwl_lcd_display_red 3 1 8\n");
	// 	num = (i * 3 + 2);
	// 	tprintf("bkwl_lcd_display_red 3 1 9 num=%d\n", num);
	// 	show_data = show_data + num;
	// 	*(show_data) = value;
	// 	// tprintf("bkwl_lcd_display_red 3 1 1\n");
	// 	// *show_data = value;
	// 	// tprintf("bkwl_lcd_display_red 3 1 2\n");
	// 	// *(show_data + 1) = value;
	// 	// tprintf("bkwl_lcd_display_red 3 1 3\n");
	// 	// *((uint8_t *)show_data + (i * 3 + 0)) = (uint8_t)((color >> 16) & 0xff);
	// 	// tprintf("bkwl_lcd_display_red 3 2\n");
	// 	// *((uint8_t *)show_data + (i * 3 + 1)) = (uint8_t)((color >> 8) & 0xff);
	// 	// tprintf("bkwl_lcd_display_red 3 3\n");
	// 	// *((uint8_t *)show_data + (i * 3 + 2)) = (uint8_t)((color >> 0) & 0xff);
	// 	// tprintf("bkwl_lcd_display_red 4\n");
	// }
	// tprintf("bkwl_lcd_display_red 5\n");

	pvShowMemoryConfigInfo1();

	uint32_t color = 0;
	bkwl_lcd_get_rand_color(&color);
	for (int i = 0; i < 800 * 480; i++)
	{
		*(addr + i) = color;
	}
	// ret = bkwl_lcd_fill(addr, show_data, width, height);
	tprintf("bkwl_lcd_display_red 6\n");
	ret = bkwl_lcd_display(addr, lcd_type_name, ppi, lcd_type, width, height);
	tprintf("bkwl_lcd_display_red 7\n");
	if (BK_OK != ret)
	{
		os_printf("%s, lcd display fail\n", __func__);
	}
	tprintf("bkwl_lcd_display_red exit\n");
	return ret;
}
